#############################################################################
#Yuhua Wang#
#The Rise and Fall of Imperial China: The Social Origins of State Development#
#Princeton University Press, 2022#
############################################################################

############################################################################
#Chapter 7#
############################################################################

######################################
#Figure 7.2: Spatial Distribution of Lineage Surnames (1801–1850)#
######################################

#Constructed in QGIS

######################################
#Figure 7.3: Exam Success, Violence, and Lineage Organizations: Scatter Plots#
######################################

library(foreign) 
library(ggplot2) 
require(ggplot2)
library(ggrepel) 
library(plyr)
require(scales)
require(gridExtra)

#Change this to your local directory#

setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Chapter 7/data/")

lineage <- read.dta("lineage cross section.dta")

attach(lineage)

plot1 <-ggplot(lineage, aes(x=jinshi_earlyqing_IHS, y=clans_IHS)) + 
  geom_point(shape=19, color="violetred4", size=3)+
  geom_smooth(method=lm,  se=TRUE,linetype="solid",
              color="deepskyblue4", fill="deepskyblue2")+
  scale_y_continuous(name="N of Lineage Organizations (IHS)",limits=c(0,4), breaks=c(0,1,2,3,4))+
  scale_x_continuous(name="N of Advanced Scholars (IHS)", limits=c(0,7), breaks=c(0,1,2,3,4,5,6,7))+
  theme(text = element_text(size=18, 
                            vjust=1, family="Times"))+
  theme_bw()


plot2 <-ggplot(lineage, aes(x=conflicts_earlyqing_IHS, y=clans_IHS)) + 
  geom_point(shape=19, color="violetred4", size=3)+
  geom_smooth(method=lm,  se=TRUE,linetype="solid",
              color="deepskyblue4", fill="deepskyblue2")+
  scale_y_continuous(name="N of Lineage Organizations (IHS)",limits=c(0,4), breaks=c(0,1,2,3,4))+
  scale_x_continuous(name="N of Conflicts (IHS)", limits=c(0,2.5), breaks=c(0,0.5,1,1.5,2,2.5))+
  theme(text = element_text(size=18, 
                            vjust=1, family="Times"))+
  theme_bw()


grid.arrange(plot1, plot2, ncol=2)


